﻿using Factory.RBAC;
using Factory.RBAC.UserRoles;
using Factory.RBAC.UserRoles.Dto;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Factory.Web.Controllers.RBAC
{
    public class UserRolesController : Controller
    {
        private readonly IUserRolesAppSerivce _userService;

        public UserRolesController(IUserRolesAppSerivce userService)
        {
            _userService = userService;
        }

        /// <summary>
        /// 查询用户所对应的角色
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        //[HttpGet]
        //public async Task<List<UserRole>> GetUserInRoles(int userId)
        //{
        //    return await _userService.GetUserInRoles(userId);
        //}


        public IActionResult Index()
        {
            return View();
        }

        public IActionResult UserRolesIndex(int userId)
        {
            ViewBag.userId = userId;
            return View();
        }


        [HttpGet]
        public async Task<IActionResult> GetUserInRoles(int userId)
        {
            var userRoles = await _userService.GetUserInRoles(userId);
            var result = userRoles.Select(ur => new { ur.RoleId,ur.RoleName }).ToList();
            return Ok(new { data = result });
        }

        [HttpPost]
        public async Task<int> AllotRoles(AddUserRoleDto dto)
        {
            var i = await _userService.AllotRoles(dto);
            return i;
        }

    }
}
